package org.dromara.hotel.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.common.core.utils.BeanUtils;
import org.dromara.hotel.domain.TerHotelOrders;
import org.dromara.hotel.domain.enums.StaticOrderStatus;
import org.dromara.hotel.mapper.TerHotelOrdersMapper;
import org.dromara.hotel.service.IWxMaNoticeService;
import org.dromara.tearoom.api.RemoteHtlOrderService;
import org.dromara.tearoom.api.domain.bo.RemoteHtlOrderBo;
import org.dromara.tearoom.api.domain.vo.RemoteHtlOrderVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

@RequiredArgsConstructor
@Service
@DubboService
public class RemoteHtlOrderServiceImpl implements RemoteHtlOrderService {
    private final TerHotelOrdersMapper baseMapper;
    private final IWxMaNoticeService wxMaNoticeService;
    @Override
    public RemoteHtlOrderVo queryOrderById(String orderId) {
      return BeanUtils.toBean(baseMapper.selectVoById(orderId),RemoteHtlOrderVo.class);
    }

    @Override
    public int updateById(RemoteHtlOrderBo bo) {
        TerHotelOrders order=BeanUtils.toBean(bo,TerHotelOrders.class);
        if(Objects.equals(order.getOrderStatus(), StaticOrderStatus.CONFIRMED)){
            wxMaNoticeService.placeOrder(bo.getId());
        }
        order.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(order);
    }

    @Override
    public Boolean resetOrderPrice(RemoteHtlOrderVo vo){
        return baseMapper.resetOrderPrice(vo);
    }


    @Override
    public int taskDelete(LocalDateTime date){
        LambdaQueryWrapper<TerHotelOrders> lqw = Wrappers.lambdaQuery();
        lqw.le(TerHotelOrders::getCreateTime,date.minusHours(1));
        lqw.eq(TerHotelOrders::getOrderStatus,StaticOrderStatus.UN_PAY);
        return baseMapper.delete(lqw);
    }
}
